<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GatewayService
 *
 * @author gaitanoboeraeve
 */
class Service_GatewayService 
{
    public function __construct()
    {
        
    }

    public function _traceToLog()
    {
        $myFile = "testFile.txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        $stringData = "Bobby Bopper\n";
        fwrite($fh, $stringData);
        $stringData = "Tracy Tanner\n";
        fwrite($fh, $stringData);
        fclose($fh);
    }
    
    
/*
 * 
 * ANNOUNCEMENT
 * 
 * 
 */    
    /**
     * @return Ahs_VO_Announcement
     */
    public function getAnnouncement()
    {
        $announcementMapper = new Application_Model_AnnouncementMapper();
        $announcement = $announcementMapper->fetchById(1);
        
        $announcementVO = $this->_toAnnouncementVO($announcement);
        return $announcementVO;
    }
    
    /**
     * @return array
     */
    public function getAnnouncements()
    {
        $announcementMapper = new Application_Model_AnnouncementMapper();
        $announcement = $announcementMapper->fetchAll();
        
        $announcementVOs = $this->_toAnnouncementVOs($announcement);
        return $announcementVOs;
    }

    /**
     * @return array
     */
    public function getAnnouncementsList()
    {
        return $this->getAnnouncements();
    }
    
    /**
     * @param Application_Model_Announcement $announcement
     * @return VO_Announcement 
     */
    private function _toAnnouncementVO(Application_Model_Announcement $announcement)
    {
        $announcementVO = new VO_Announcement();
        $announcementVO->id         = $announcement->Id;
        $announcementVO->title      = $announcement->Title;
        $announcementVO->text      = $announcement->Text;
        $announcementVO->creationdate      = $announcement->Creationdate;
        return $announcementVO;
    }
    
    /**
     * @param array $announcement
     * @return array
     */
    private function _toAnnouncementVOs($announcements = array())
    {
        $announcementVOs = array();
        foreach ($announcements as $announcement) {
            $announcementVOs[] = $this->_toAnnouncementVO($announcement);
        }
        
        return $announcementVOs;
    }
    
    
/*
 * 
 * Article
 * 
 * 
 */     
    
    /**
     * @return Ahs_VO_Article
     */
    public function getArticle()
    {
        $articleMapper = new Application_Model_ArticleMapper();
        $article = $articleMapper->fetchById(1);
        
        $articleVO = $this->_toArticleVO($article);
        return $articleVO;
    }
    
    /**
     * @return array
     */
    public function getArticles()
    {
        $articleMapper = new Application_Model_ArticleMapper();
        $article = $articleMapper->fetchAll();
        
        $articleVOs = $this->_toArticleVOs($article);
        return $articleVOs;
    }
    
    public function getArticlesByChapterId($zendChapterId)
    {
        $articleMapper = new Application_Model_ArticleMapper();
        $article = $articleMapper->fetchByChapterId($zendChapterId);
        
        $articleVOs = $this->_toArticleVOs($article);
        return $articleVOs;
    }

    /**
     * @return array
     */
    public function getArticlesList()
    {
        return $this->getArticles();
    }
    
     /**
     * @return array
     */
    public function getArticlesListByChapterId($zendChapterId)
    {
        return $this->getArticlesByChapterId($zendChapterId);
    }
    
    /**
     * @param Application_Model_Article $article
     * @return VO_Article 
     */
    private function _toArticleVO(Application_Model_Article $article)
    {
        $articleVO = new VO_Article();
        $articleVO->id                = $article->Id;
        $articleVO->name              = $article->Name;
        $articleVO->text              = $article->Text;
        $articleVO->creationdate      = $article->Creationdate;
        $articleVO->modifieddate      = $article->Modifieddate;        
        return $articleVO;
    }
    
    /**
     * @param array $article
     * @return array
     */
    private function _toArticleVOs($articles = array())
    {
        $articleVOs = array();
        foreach ($articles as $article) {
            $articleVOs[] = $this->_toArticleVO($article);
        }
        
        return $articleVOs;
    }



/*
 * 
 * AUTHORS
 * 
 * 
 */    
    /**
     * @return Ahs_VO_User
     */
    public function getAuthor()
    {
        
        $userMapper = new Application_Model_UserMapper();
        $user = $userMapper->fetchById(1); 
        $userVO = $this->_toUserVO($user);
        return $userVO;  
    }
    
    

/*
 * 
 * BOOK
 * 
 * 
 */         
    /**
     * @return Ahs_VO_Book
     */
    public function getBook($zendBookId)
    {
        $bookMapper = new Application_Model_BookMapper();
        $book = $bookMapper->fetchById($zendBookId);
        
        $bookVO = $this->_toBookVO($book);
        return $bookVO;
    }
    
    /**
     * @return array
     */
    public function getBooks($zendCourseId)
    {
        $bookMapper = new Application_Model_BookMapper();
        $book = $bookMapper->fetchByCourseId($zendCourseId);
        
        $bookVOs = $this->_toBookVOs($book);
        return $bookVOs;
    }
    
    /**
     * @return array
     */
    public function getBooksByUserId($zendUserId)
    {
        $bookMapper = new Application_Model_BookMapper();
        $book = $bookMapper->fetchByCourseId($zendUserId);
        
        $bookVOs = $this->_toBookVOs($book);
        return $bookVOs;
    }

    /**
     * @return array
     */
    public function getBooksListByUserId($zendUserId)
    {
        return $this->getBooksByUserId($zendUserId);
    }
    

    /**
     * @return array
     */
    public function getBooksList($zendCourseId)
    {
        return $this->getBooks($zendCourseId);
    }
    
    
    /**
     * @param Application_Model_Book $book
     * @return VO_Book 
     */
    private function _toBookVO(Application_Model_Book $book)
    {
        $author_id = $book->AuthorId;
        $bookVO = new VO_Book();
        $bookVO->id         = $book->Id;
        $bookVO->author_id         = $book->AuthorId;
        $bookVO->title      = $book->Title;
        $bookVO->description= $book->Description;
        $bookVO->author  = "test";
        
        return $bookVO;
    }
    
    /**
     * @param array $book
     * @return array
     */
    private function _toBookVOs($books = array())
    {
        $bookVOs = array();
        foreach ($books as $book) {
            $bookVOs[] = $this->_toBookVO($book);
        }
        
        return $bookVOs;
    }
    
    
    
    
    
/*
 * 
 * CHAPTER
 * 
 * 
 */     
    /**
     * @return Ahs_VO_Chapter
     */
    public function getChapter()
    {
        $chapterMapper = new Application_Model_ChapterMapper();
        $chapter = $chapterMapper->fetchById(1);
        
        $chapterVO = $this->_toChapterVO($chapter);
        return $chapterVO;
    }
    
    /**
     * @return array
     */
    public function getChapters()
    {
        $chapterMapper = new Application_Model_ChapterMapper();
        $chapter = $chapterMapper->fetchAll();
        
        $chapterVOs = $this->_toChapterVOs($chapter);
        return $chapterVOs;
    }
    
    public function getChaptersByBookId()
    {
        $chapterMapper = new Application_Model_ChapterMapper();
        $chapter = $chapterMapper->fetchByBookId(5);
        
        $chapterVOs = $this->_toChapterVOs($chapter);
        return $chapterVOs;
    }

    /**
     * @return array
     */
    public function getChaptersList()
    {
        return $this->getChapters();
    }
    
    /**
     * @return array
     */
    public function getChaptersListByBookId()
    {
        return $this->getChapters();
    }
    
    /**
     * @param Application_Model_Chapter $chapter
     * @return VO_Chapter 
     */
    private function _toChapterVO(Application_Model_Chapter $chapter)
    {
        $chapterVO = new VO_Chapter();
        $chapterVO->id                = $chapter->Id;
        $chapterVO->name              = $chapter->Name;
        $chapterVO->creationdate      = $chapter->Creationdate;
        $chapterVO->modifieddate      = $chapter->Modifieddate;
        return $chapterVO;
    }
    
    /**
     * @param array $chapter
     * @return array
     */
    private function _toChapterVOs($chapters = array())
    {
        $chapterVOs = array();
        foreach ($chapters as $chapter) {
            $chapterVOs[] = $this->_toChapterVO($chapter);
        }
        
        return $chapterVOs;
    }
    

    
    
    
/*
 * 
 * COMMENT
 * 
 * 
 */     
    /**
     * @return Ahs_VO_Comment
     */
    public function getComment()
    {
        $commentMapper = new Application_Model_CommentMapper();
        $comment = $commentMapper->fetchById(1);
        
        $commentVO = $this->_toCommentVO($comment);
        return $commentVO;
    }
    
    public function setComment($commentVO)
    {
        
        $comment = new Application_Model_Comment();
        /*
        //OPVANGEN ADHV ARRAY DATA
        $comment->Text = $commentVO["text"];
        $comment->Subject = $commentVO["subject"];
        */
        //OPVANGEN ADHV OBJECT DATA
        $comment->Text = $commentVO->text;
        $comment->Subject = $commentVO->subject;
        
        $commentMapper = new Application_Model_CommentMapper();
        $commentMapper->save($comment);
        
    }
    
    /**
     * @return array
     */
    public function getComments()
    {
        $commentMapper = new Application_Model_CommentMapper();
        $comment = $commentMapper->fetchAll();
        
        $commentVOs = $this->_toCommentVOs($comment);
        return $commentVOs;
    }


    /**
     * @return array
     */
    public function getCommentsList()
    {
        return $this->getComments();
    }
    
    /**
     * @param Application_Model_Comment $comment
     * @return VO_Comment 
     */
    private function _toCommentVO(Application_Model_Comment $comment)
    {
        $commentVO = new VO_Comment();
        $commentVO->id         = $comment->Id;
        $commentVO->subject      = $comment->Subject;
        $commentVO->text         = $comment->Text;
        $commentVO->creationdate      = $comment->Creationdate;

        
        return $commentVO;
    }
    
    /**
     * @param array $comment
     * @return array
     */
    private function _toCommentVOs($comments = array())
    {
        $commentVOs = array();
        foreach ($comments as $comment) {
            $commentVOs[] = $this->_toCommentVO($comment);
        }
        
        return $commentVOs;
    }
    

    
    
    
    
/*
 * 
 * COURSES
 * 
 * 
 */    
    public function getCourseTest()
    {
       //doe niets
    }
    /**
     * @return Ahs_VO_Course
     */
    public function getCourse()
    {
        $courseMapper = new Application_Model_CourseMapper();
        $course = $courseMapper->fetchById(1);
        
        $courseVO = $this->_toCourseVO($course);
        return $courseVO;
    }
    
    /**
     * @return array
     */
    public function getCourses()
    {
        $courseMapper = new Application_Model_CourseMapper();
        $courses = $courseMapper->fetchAll();
        
        $courseVOs = $this->_toCourseVOs($courses);
        return $courseVOs;
    }
    
    public function getMyCourses($user_id)
    {
        $courseMapper = new Application_Model_CourseMapper();
        $courses = $courseMapper->fetchReadingListByUserId($user_id);
        
        $courseVOs = $this->_toCourseVOs($courses);
        return $courseVOs;
    }

    /**
     * @return array
     */
    public function getCoursesList()
    {
        return $this->getCourses();
    }
    /**
     * @return array
     */
    public function getMyCoursesList($user_id)
    {
        return $this->getMyCourses($user_id);
    }
    
    /**
     * @param Application_Model_Course $course
     * @return VO_Course 
     */
    private function _toCourseVO(Application_Model_Course $course)
    {  
        $courseVO = new VO_Course();
        $courseVO->id            = $course->Id;
        $courseVO->name          = $course->Name;
        $courseVO->author_id     = $course->Authorid;
        return $courseVO;
    }
    
    /**
     * @param array $course
     * @return array
     */
    private function _toCourseVOs($courses = array())
    {
        $courseVOs = array();
        foreach ($courses as $course) {
            $courseVOs[] = $this->_toCourseVO($course);
        }
        
        return $courseVOs;
    }
    
    

    
    
/*
 * 
 * MESSAGES
 * 
 * 
 */     
    /**
     * @return Ahs_VO_Message
     */
    public function getMessage()
    {
        $messageMapper = new Application_Model_MessageMapper();
        $message = $messageMapper->fetchById(1);
        
        $messageVO = $this->_toMessageVO($message);
        
        return $messageVO;
    }
    
    /**
     * @return array
     */
    public function getMessages()
    {
        $messageMapper = new Application_Model_MessageMapper();
        $messages = $messageMapper->fetchAll();
        
        $messageVOs = $this->_toMessageVOs($messages);

        return $messageVOs;
    }

    /**
     * @return array
     */
    public function getMessagesList()
    {
        return $this->getMessages();
    }
    
    /**
     * @param Application_Model_Message $message
     * @return VO_Message 
     */
    private function _toMessageVO(Application_Model_Message $message)
    {
        $messageVO = new VO_Message();
        $messageVO->id         = $message->Id;
        $messageVO->userto      = $message->Userto;
        $messageVO->userfrom  = $message->Userfrom;
        $messageVO->senddate = $message->Senddate;
        $messageVO->subject  = $message->Subject;
        $messageVO->content = $message->Content;
        
        return $messageVO;
    }
    
    /**
     * @param array $messages
     * @return array
     */
    private function _toMessageVOs($messages = array())
    {
        $messageVOs = array();
        foreach ($messages as $message) {
            $messageVOs[] = $this->_toMessageVO($message);
        }
        
        return $messageVOs;
    }
    
    
    
/*
 * 
 * USERS
 * 
 * 
 */     
    /**
     * @return Ahs_VO_User
     */
    public function getUser()
    {
        $userMapper = new Application_Model_UserMapper();
        $user = $userMapper->fetchById(1); 
        $userVO = $this->_toUserVO($user);
        return $userVO;
    }
    
    /**
     * @return Ahs_VO_User
     */
    public function getUserByEmail($zendUserEmail)
    {
        $userMapper = new Application_Model_UserMapper();
        $user = $userMapper->fetchByEmail($zendUserEmail); 
        $userVO = $this->_toUserVO($user);
        return $userVO;
    }
    
    /**
     * @return array
     */
    public function getUsers()
    {
        $userMapper = new Application_Model_UserMapper();
        $users = $userMapper->fetchAll();
        
        $userVOs = $this->_toUserVOs($users);

        return $userVOs;
    }

    /**
     * @return array
     */
    public function getUsersList()
    {
        return $this->getUsers();
    }
    
    /**
     * @param Application_Model_User $user
     * @return VO_User 
     */
    private function _toUserVO(Application_Model_User $user)
    {
        
        $userVO = new VO_User();
        $userVO->id         = $user->Id;
        $userVO->email      = "blabla";
        $userVO->password   = "blabla";
        $userVO->givenname  = $user->Givenname;   
        $userVO->familyname = $user->Familyname;
        $userVO->addressline = $user->Addressline;
        $userVO->postcode = $user->Postcode;
        
        $country = new Application_Model_Country();
        $countryMapper = new Application_Model_CountryMapper();
        $country = $countryMapper->fetchById($user->CountryId);
        
        $userVO->country = $country->Name;
        return $userVO;
        
    }
    
    /**
     * @param array $users
     * @return array
     */
    private function _toUserVOs($users = array())
    {
        $userVOs = array();
        foreach ($users as $user) {
            $userVOs[] = $this->_toUserVO($user);
        }
        
        return $userVOs;
    }
}
?>
